import java.io.*;
import java.net.Socket;
public class ClientSocketDemo{
	
	private Socket socket = null ;

	public void talk() throws Exception{

		try{
			socket = new Socket("localhost",8000);			
		
			PrintWriter socketWriter = getWriter(socket);
			BufferedReader socketReader = getReader(socket);

			BufferedReader localReader = new BufferedReader(new InputStreamReader(System.in));

			String msg = null ;

			while((msg = localReader.readLine()) != null){
				socketWriter.println(msg);

				System.out.println(socketReader.readLine());  
				if("bye".equals(msg)){
					break ;
				}
			}
		}catch(Exception e){
			e.printStackTrace();
			try{
				socket.close();
			}catch(Exception ex){}
		}
	}

	public PrintWriter getWriter(Socket socket) throws Exception{
		OutputStream socketOut = socket.getOutputStream();
		return new PrintWriter(socketOut,true);
	}

	public BufferedReader getReader(Socket socket) throws Exception{
		return new BufferedReader(new InputStreamReader(socket.getInputStream()));
	}

	public static void main(String[] args) throws Exception{
		new ClientSocketDemo().talk();	
	}
}
